<template>
  <el-dialog :title="titleName" :visible.sync="isShow" width="500px" append-to-body>
    <el-form ref="form" :model="documentInfo" :rules="rules" label-width="80px">
      <el-form-item label="文档类型ID" prop="docuTypeId">
        <el-input
          v-model="documentInfo.docuTypeId"
          placeholder="请输入文档类型ID"
        />
      </el-form-item>
      <el-form-item label="文档名称" prop="docuName">
        <el-input
          v-model="documentInfo.docuName"
          placeholder="请输入文档名称"
        />
      </el-form-item>
      <el-form-item label="文档简介" prop="docuDesc">
        <el-input
          v-model="documentInfo.docuDesc"
          placeholder="请输入文档简介"
        />
      </el-form-item>
      <el-form-item label="文档版本" prop="docuVersion">
        <el-input
          v-model="documentInfo.docuVersion"
          placeholder="请输入文档版本"
        />
      </el-form-item>
      <el-form-item label="文档minio地址" prop="docuAddr">
        <el-input
          v-model="documentInfo.docuAddr"
          placeholder="请输入文档minio地址"
        />
      </el-form-item>
      <el-form-item
        label="指定文档角色组(与其他权限校验字段是或的关系)"
        prop="docuRole"
      >
        <el-input
          v-model="documentInfo.docuRole"
          placeholder="请输入指定文档角色组(与其他权限校验字段是或的关系)"
        />
      </el-form-item>
      <el-form-item
        label="指定文档部门组(与其他权限校验字段是或的关系)"
        prop="docuDept"
      >
        <el-input
          v-model="documentInfo.docuDept"
          placeholder="请输入指定文档部门组(与其他权限校验字段是或的关系)"
        />
      </el-form-item>
      <el-form-item
        label="指定人员查看(与其他权限校验字段是或的关系)"
        prop="docuUser"
      >
        <el-input
          v-model="documentInfo.docuUser"
          placeholder="请输入指定人员查看(与其他权限校验字段是或的关系)"
        />
      </el-form-item>
      <!-- <el-form-item label="是否删除(1:存在;0:删除)" prop="docuDel">
        <el-input
          v-model="documentInfo.docuDel"
          placeholder="请输入是否删除(1:存在;0:删除)"
        />
      </el-form-item> -->
      <el-form-item label="备注" prop="docuTemp">
        <el-input v-model="documentInfo.docuTemp" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  listDocument,
  getDocument,
  delDocument,
  addDocument,
  updateDocument,
} from "@/api/doc/document";
import { DIALOG_TYPE } from "@/utils/const.js"

export default {
  name: "DocumentEditDialog",
  computed: {
    titleName() {
      switch (this.dialogType) {
        case DIALOG_TYPE.ADD:
          return "添加文档详情";
        case DIALOG_TYPE.UPDATE:
          return "编辑文档详情";
        case DIALOG_TYPE.DETAIL:
          return "文档详情";
        default:
          return "添加文档详情";
      }
    },
  },
  props: {
    isShow: Boolean,
    dialogType: String,
    docuId: String
  },
  data() {
    return {
      // 表单
      documentInfo: {},
      // 表单校验
      rules: {
        docuTypeId: [
          { required: true, message: "文档类型ID不能为空", trigger: "blur" },
        ],
        docuName: [
          { required: true, message: "文档名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    cancel() {
      this.reset();
      this.closeDialog();
    },
    closeDialog() {
      this.$emit("close");
    },
    updateList() {
      this.$emit("updateList");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.docuId != null) {
            updateDocument(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.closeDialog();
              this.updateList();
            });
          } else {
            addDocument(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.closeDialog();
              this.updateList();
            });
          }
        }
      });
    },
    // 表单重置
    reset() {
      this.documentInfo = {
        docuId: null,
        docuTypeId: null,
        docuName: null,
        docuDesc: null,
        docuVersion: null,
        docuAddr: null,
        docuIndex: null,
        docuCreater: null,
        docuCreateTime: null,
        docuRole: null,
        docuDept: null,
        docuUser: null,
        docuDel: null,
        docuTemp: null,
      };
      this.resetForm("form");
    },
    updateInfo() {
      this.reset()
    }
  },
  watch: {
    dialogType(val, oldVal) {
      this.updateInfo()
      console.log(val, oldVal)
      if(val == "update") {
        getDocument(this.docuId).then((response) => {
          this.documentInfo = response.data;
        });
      }
    }
  }
};
</script>

<style></style>
